<template>
<div>
  <h2>{{title}}</h2>
  <h3>试卷预览</h3>
  <br>
  <div id="paper" v-for="(part, partIndex) in paper.parts">
    <h4 v-if="!partEditFlags[partIndex]" @click="partEditFlags[partIndex] = true & $set(partEditFlags, partIndex, true)">{{part.title}}</h4>
    <div v-else>
      <input type="text" v-model="part.title">
      <button @click="partEditFlags[partIndex] = false & $set(partEditFlags, partIndex, false)">退出编辑</button>
    </div>
    <br>
    <ul>
      <li v-for="(task, taskIndex) in part.tasks">
        <div v-if="!editFlags[partIndex + '.' + taskIndex]">
          <span style="font-weight: 700;">
            {{taskIndex + 1}}. {{task.title}}
          </span>
          <div v-if="task.type === TYPE.SINGLE">
            <ul>
              <li v-for="(option, optionIndex) in task.options" :style="{color: task.answer === optionIndex ? 'green' : ''}">
                {{String.fromCharCode('A'.charCodeAt(0) + optionIndex)}}. {{option.label}}
              </li>
            </ul>
          </div>
          <div v-else-if="task.type === TYPE.MULTI">
            <ul>
              <li v-for="(option, optionIndex) in task.options" :style="{color: task.answer.indexOf(optionIndex) !== -1 ? 'green' : ''}">
                {{String.fromCharCode('A'.charCodeAt(0) + optionIndex)}}. {{option.label}}
              </li>
            </ul>
          </div>
          <div v-else-if="task.type === TYPE.ANSWER">
            <textarea name="" id="" cols="30" rows="10"></textarea>
          </div>
          <button @click="moveTask(part.tasks, taskIndex, -1)">上移</button>
          <button @click="moveTask(part.tasks, taskIndex, 1)">下移</button>
          <button @click="editFlags[partIndex + '.' + taskIndex] = true & $set(editFlags, partIndex + '.' + taskIndex, true)">编辑该题</button>
          <button @click="part.tasks.splice(taskIndex, 1)">删除该题</button>
        </div>
        <div v-else>
          编辑中
          {{task.type}}
          <select name="type" id="task-type" v-model="task.type" @change="toggleType(task)">
            <option label="单选题" :value="1">single</option>
            <option label="多选题" :value="2">multi</option>
            <option label="简答题" :value="3">answer</option>
          </select>
          <!-- 单选编辑 -->
          <div v-if="task.type === TYPE.SINGLE">
            单选编辑界面
            <br>
            题面：<input type="text" v-model="task.title">
            <br>
            选项：
            <br>
            <span v-for="(item, index) in task.options">
              [选项{{index + 1}}]: <input type="text" v-model="item.label">
              <button @click="task.options.splice(index, 1)">删除</button>
              <br>
            </span>
            <button @click="task.options.push({})">添加选项</button>
            <br>
            正确答案：
            <select name="right-answer" id="1" @select="console.log('1')" v-model="task.answer">
              <option :value="optionIndex" v-for="(option, optionIndex) in task.options" :label="`[选项${optionIndex + 1}]`"></option>
            </select>
          </div>
          <!-- 多选编辑 -->
          <div v-else-if="task.type === TYPE.MULTI">
            多选编辑界面
            <br>
            题面：<input type="text" v-model="task.title">
            <br>
            选项：
            <br>
            <span v-for="(item, index) in task.options">
              [选项{{index + 1}}]: <input type="text" v-model="item.label">
              <button @click="task.options.splice(index, 1)">删除</button>
              <br>
            </span>
            <button @click="task.options.push({})">添加选项</button>
            <br>
            正确答案：
            <br>
            <span v-for="(option, optionIndex) in task.options">
              <input type="checkbox" :value="option.label" :id="'checkbox' + partIndex + taskIndex + optionIndex" @click="multiCheck($event, task, optionIndex)"/>
              <label :for="'checkbox' + partIndex + taskIndex + optionIndex">[选项{{optionIndex + 1}}]</label>
              <br>
            </span>
<!--            <select name="right-answer" id="1" @select="console.log('1')" v-model="task.answer">-->
<!--              <option :value="optionIndex" v-for="(option, optionIndex) in task.options" :label="`[选项${optionIndex + 1}]`"></option>-->
<!--            </select>-->
          </div>
          <!-- 简答编辑 -->
          <div v-else-if="task.type === TYPE.ANSWER">
            简答编辑界面
            <br>
            题面：<input type="text" v-model="task.title">
            <br>
            参考答案：<br>
            <textarea name="" id="" cols="30" rows="10" v-model="task.answer"></textarea>
          </div>
          <button @click="editFlags[partIndex + '.' + taskIndex] = false | $set(editFlags, partIndex + '.' + taskIndex, false)">退出编辑</button>
        </div>
        <br>
      </li>
    </ul>
    <button @click="part.tasks.push(JSON.parse(JSON.stringify(DEFAULT_TASK)))">添加题目</button>
  </div>
  <button @click="paper.parts.push(JSON.parse(JSON.stringify(DEFAULT_PART)))">添加部分</button>
</div>
</template>

<script>
const TYPE = {
  SINGLE: 1,
  MULTI: 2,
  ANSWER: 3
}
const DEFAULT_TASK = {
  type: TYPE.SINGLE,
  score: 5,
  title: '题面',
  answer: 0,
  options: [{
    label: '选项1',
  }, {
    label: '选项2',
  }]
}
const DEFAULT_PART = {
  title: '部分标题',
  tasks: [JSON.parse(JSON.stringify(DEFAULT_TASK))]
}
export default {
  name: "index",
  data: _ => ({
    TYPE,
    DEFAULT_TASK,
    DEFAULT_PART,
    title: '组卷',
    msg: 'ok',
    curType: 1,
    editFlags: [],
    partEditFlags: [],
    paper: {
      parts: [{
        title: '单选题',
        tasks: [{
          type: TYPE.SINGLE,
          score: 5,
          title: '为什么1+1=2?',
          answer: 0,
          options: [{
            label: '这是正确选项',
          }, {
            label: '这是错误选项1',
          }, {
            label: '这是错误选项2',
          }]
        }, {
          type: TYPE.SINGLE,
          score: 5,
          title: '为什么1+2=3?',
          answer: 1,
          options: [{
            label: '这是正确选项',
          }, {
            label: '这是错误选项1',
          }, {
            label: '这是错误选项2',
          }]
        }]
      }],
    },
  }),
  methods: {
    addTask () {
      if (this.curType === 1) {
        console.log(this.single)
      }
    },
    toggleType (task) {
      switch (task.type) {
        case TYPE.SINGLE:
          task.answer = null
          break
        case TYPE.MULTI:
          task.answer = []
          break
        case TYPE.ANSWER:
          task.answer = ''
          break
        default:
          break
      }
    },
    /**
     * 移动题目
     *
     * @param tasks 本部分
     * @param taskIndex 任务所处index
     * @param direction 方向
     */
    moveTask (tasks, taskIndex, direction) {
      let targetIndex = taskIndex + direction
      targetIndex = Math.max(0, targetIndex)
      targetIndex = Math.min(targetIndex, tasks.length - 1)
      let targetItem = tasks[targetIndex]
      this.$set(tasks, targetIndex, tasks[taskIndex])
      this.$set(tasks, taskIndex, targetItem)
    },

    multiCheck (e, task, optionIndex) {
      if (e.target.checked) {
        task.answer.push(optionIndex)
      } else {
        task.answer.splice(task.answer.indexOf(optionIndex), 1)
      }
    }
  }
}
</script>

<style scoped>

</style>